<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::Inheritance::ClassMethods</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::Inheritance::ClassMethods 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activerecord/lib/active_record/inheritance_rb.html">activerecord/lib/active_record/inheritance.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-abstract_class-3F">abstract_class?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>B</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-base_class">base_class</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-compute_type">compute_type</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>D</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-descends_from_active_record-3F">descends_from_active_record?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-new">new</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-sti_name">sti_name</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-symbolized_base_class">symbolized_base_class</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-symbolized_sti_name">symbolized_sti_name</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>abstract_class</td>
            <td class='attr-desc'><p>Set this to true if this is an abstract class (see
<code>abstract_class?</code>). If you are using inheritance with <a
href="../../ActiveRecord.html">ActiveRecord</a> and don’t want child
classes to utilize the implied STI table name of the parent class, this
will need to be true. For example, given the following:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">SuperClass</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">abstract_class</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Child</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">SuperClass</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">table_name</span> = <span class="ruby-string">'the_table_i_really_want'</span>
<span class="ruby-keyword">end</span>
</pre>

<p><code>self.abstract_class = true</code> is required to make
<code>Child&lt;.find,.create, or any Arel method&gt;</code> use
<code>the_table_i_really_want</code> instead of a table called
<code>super_classes</code></p></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-abstract_class-3F">
            
              <b>abstract_class?</b>()
            
            <a href="ClassMethods.html#method-i-abstract_class-3F" name="method-i-abstract_class-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns whether this class is an abstract class or not.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-abstract_class-3F_source')" id="l_method-i-abstract_class-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L92" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-abstract_class-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 92</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">abstract_class?</span>
  <span class="ruby-keyword">defined?</span>(<span class="ruby-ivar">@abstract_class</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@abstract_class</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-base_class">
            
              <b>base_class</b>()
            
            <a href="ClassMethods.html#method-i-base_class" name="method-i-base_class" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the class descending directly from <a
href="../Base.html">ActiveRecord::Base</a>, or an abstract class, if any,
in the inheritance hierarchy.</p>

<p>If A extends AR::Base, A.base_class will return A. If B descends from A
through some arbitrarily deep hierarchy, B.base_class will return A.</p>

<p>If B &lt; A and C &lt; B and if A is an <a
href="ClassMethods.html#attribute-i-abstract_class">#abstract_class</a>
then both B.base_class and C.base_class would return B as the answer since
A is an abstract_class.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-base_class_source')" id="l_method-i-base_class_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L62" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-base_class_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 62</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">base_class</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-keyword">self</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Base</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ActiveRecordError</span>, <span class="ruby-node">&quot;#{name} doesn't belong in a hierarchy descending from ActiveRecord&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">superclass</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Base</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">superclass</span>.<span class="ruby-identifier">abstract_class?</span>
    <span class="ruby-keyword">self</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">superclass</span>.<span class="ruby-identifier">base_class</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-descends_from_active_record-3F">
            
              <b>descends_from_active_record?</b>()
            
            <a href="ClassMethods.html#method-i-descends_from_active_record-3F" name="method-i-descends_from_active_record-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>True if this isn’t a concrete subclass needing a STI type condition.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-descends_from_active_record-3F_source')" id="l_method-i-descends_from_active_record-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L31" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-descends_from_active_record-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 31</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">descends_from_active_record?</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Base</span>
    <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">superclass</span>.<span class="ruby-identifier">abstract_class?</span>
    <span class="ruby-identifier">superclass</span>.<span class="ruby-identifier">descends_from_active_record?</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">superclass</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Base</span> <span class="ruby-operator">||</span> <span class="ruby-operator">!</span><span class="ruby-identifier">columns_hash</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">inheritance_column</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-new">
            
              <b>new</b>(*args, &amp;block)
            
            <a href="ClassMethods.html#method-i-new" name="method-i-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Determines if one of the attributes passed in is the inheritance column,
and if the inheritance column is attr accessible, it initializes an
instance of the given subclass instead of the base class</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-new_source')" id="l_method-i-new_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L17" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 17</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">new</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">abstract_class?</span> <span class="ruby-operator">||</span> <span class="ruby-keyword">self</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Base</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-node">&quot;#{self} is an abstract class and can not be instantiated.&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> (<span class="ruby-identifier">attrs</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>).<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">subclass</span> = <span class="ruby-identifier">subclass_from_attrs</span>(<span class="ruby-identifier">attrs</span>)
      <span class="ruby-keyword">return</span> <span class="ruby-identifier">subclass</span>.<span class="ruby-identifier">new</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># Delegate to the original .new</span>
  <span class="ruby-keyword">super</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-sti_name">
            
              <b>sti_name</b>()
            
            <a href="ClassMethods.html#method-i-sti_name" name="method-i-sti_name" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-sti_name_source')" id="l_method-i-sti_name_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L96" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-sti_name_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 96</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">sti_name</span>
  <span class="ruby-identifier">store_full_sti_class</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">name</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">demodulize</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-symbolized_base_class">
            
              <b>symbolized_base_class</b>()
            
            <a href="ClassMethods.html#method-i-symbolized_base_class" name="method-i-symbolized_base_class" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-symbolized_base_class_source')" id="l_method-i-symbolized_base_class_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L46" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-symbolized_base_class_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 46</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">symbolized_base_class</span>
  <span class="ruby-ivar">@symbolized_base_class</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">base_class</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_sym</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-symbolized_sti_name">
            
              <b>symbolized_sti_name</b>()
            
            <a href="ClassMethods.html#method-i-symbolized_sti_name" name="method-i-symbolized_sti_name" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-symbolized_sti_name_source')" id="l_method-i-symbolized_sti_name_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L50" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-symbolized_sti_name_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 50</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">symbolized_sti_name</span>
  <span class="ruby-ivar">@symbolized_sti_name</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">sti_name</span>.<span class="ruby-identifier">present?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">sti_name</span>.<span class="ruby-identifier">to_sym</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">symbolized_base_class</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
              
      <div class="sectiontitle">Instance Protected methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-compute_type">
            
              <b>compute_type</b>(type_name)
            
            <a href="ClassMethods.html#method-i-compute_type" name="method-i-compute_type" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the class type of the record using the current module as a prefix.
So descendants of MyApp::Business::Account would appear as
MyApp::Business::AccountSubclass.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-compute_type_source')" id="l_method-i-compute_type_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ab157ac6bef3e1aa9c3a221011c5dd7024f14d7e/activerecord/lib/active_record/inheritance.rb#L104" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-compute_type_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/inheritance.rb, line 104</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">compute_type</span>(<span class="ruby-identifier">type_name</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">type_name</span>.<span class="ruby-identifier">match</span>(<span class="ruby-regexp">/^::/</span>)
    <span class="ruby-comment"># If the type is prefixed with a scope operator then we assume that</span>
    <span class="ruby-comment"># the type_name is an absolute reference.</span>
    <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Dependencies</span>.<span class="ruby-identifier">constantize</span>(<span class="ruby-identifier">type_name</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-comment"># Build a list of candidates to search for</span>
    <span class="ruby-identifier">candidates</span> = []
    <span class="ruby-identifier">name</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-regexp">/::|$/</span>) { <span class="ruby-identifier">candidates</span>.<span class="ruby-identifier">unshift</span> <span class="ruby-node">&quot;#{$`}::#{type_name}&quot;</span> }
    <span class="ruby-identifier">candidates</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">type_name</span>

    <span class="ruby-identifier">candidates</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">candidate</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">begin</span>
        <span class="ruby-identifier">constant</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Dependencies</span>.<span class="ruby-identifier">constantize</span>(<span class="ruby-identifier">candidate</span>)
        <span class="ruby-keyword">return</span> <span class="ruby-identifier">constant</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">candidate</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">constant</span>.<span class="ruby-identifier">to_s</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">NameError</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
        <span class="ruby-comment"># We don't want to swallow NoMethodError &lt; NameError errors</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">instance_of?</span>(<span class="ruby-constant">NameError</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">raise</span> <span class="ruby-constant">NameError</span>, <span class="ruby-node">&quot;uninitialized constant #{candidates.first}&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    